<?php
include 'base.php';
include 'requireLogin.php';
include 'connectdb.php';


if(isset($_POST)){
    if(isset($_POST['code']) && isset($_POST['name']) && isset($_POST['infor'])){
        $sql="INSERT INTO customer(code, name, information)
        VALUES('$_POST[code]', '$_POST[name]', '$_POST[infor]')";
        if (mysql_query($sql, $co)){
            echo "Tài khoản đã thêm thành công!";
        }
        else{                  
                header("HTTP/1.0 404 Not Found");
                header('HTTP', true, 500);
            }                
    }
}
?>

<script language="JavaScript" type="text/javascript">

    $(document).ready(function () {
        $('#success, #error').hide();
        $("#submit").click(function(){            
            if($("#user").val() == ''){
                alert("Chưa điền tên đăng nhập");
                return false;
            }
            var code = $('#code').val(),                
                name = $('#name').val(),                
                infor = $('#infor').val();
                
            $.ajax({
                type: "post",
                url: "",
                data: "code=" + code + "&name=" + name + "&infor=" + infor,                
                success: function () {                    
                    $('#error').hide();                
                    $('#success').show();
                    var newRow = $("<tr><td></td><td>" + code + "</td><td>" + name + "</td><td>" + infor + "</td><td>0</td></tr>");
                    $("#list_table").append(newRow);
                    
                },
                error: function() {                       
                    $('#error').show();  
                    $('#success').hide();                    
                }                
            }); 
            return false;
        });
        
        
    });

</script>

<h2>Thêm khách hàng</h2>
<div id = "error">Mã khách hàng đã tồn tại</div>
<div id = "success">Đã thêm thành công</div>
<form method="post" action="">    
        <div class="form_settings">                    
            <p> <span>Mã khách hàng</span> <input type="text" name="code" id = "code"></p>
            <p> <span>Họ tên</span> <input type="text" name="name" id = "name"></p>
            <p> <span>Thông tin</span><textarea type="text" name="infor" id = "infor"/></textarea></p>                                   
            <span>&nbsp</span><input class="submit" type="submit" id = "submit" name="add_button" value="Thêm" />
        </div>
    </form>
<?php
$query ='SELECT
        code, name, information, total_sale
        FROM 
        customer
        ORDER BY
        code ASC';
$result = mysql_query($query, $co) or die(mysql_error($co));

echo '<table id = "list_table" class = "table" style="table-layout:auto;">';
?>
    <tr>
        <th>STT</th>
        <th>Mã khách hàng</th>
        <th>Họ tên</th>        
        <th>Thông tin</th>        
        <th>Tổng giao dịch</th>        
    </tr>
<?php
$i = 1;
while ($row = mysql_fetch_assoc($result)){
    echo '<tr><td>' .$i++ . '</td>';     
    foreach ($row as $value) {
        echo '<td>' .$value . '</td>';
    }
    echo '</tr>';
}
echo '</table>';
?>
<?php include 'footer.php';?>